package com.example.springbootspringsession.controller;

import com.example.springbootspringsession.vo.Result;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("test")
public class TestController {

    @GetMapping("hi")
    public String hi(String name) {
        return "hi, " + name;
    }

    @GetMapping("/setSession")
    public String setSession(@NotBlank(message = "zzzzzzzzzz") String username, @NotBlank(message = "zzzzzzzzzz") String password, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.setAttribute("username","刘秀辉");
        return "设置成功";
    }

    @GetMapping("/getSession")
    public Result getSession(@NotBlank(message = "zzzzzzzzzz") String username, @NotBlank(message = "zzzzzzzzzz") String password, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String username1 = (String)session.getAttribute("username");

        return Result.ok(username1);
    }
}
